const Web3 = require("web3")
const shelljs = require("shelljs");
/**
 * 用于离线签名
 * 测试账户:three proof decorate oil solution injury knock weather proof copper join rabbit
 *
 * @type {Transaction}
 */
const Tx = require('ethereumjs-tx').Transaction;
/**
 * 合约地址:合约部署后,可以从 ganache 客户端获取
 * @type {string}
 */
const address = '0x8FCF55d320B6a7D5f712f795DC55a80965fcF8eE';

const fs = require('fs')
const source = fs.readFileSync('/Users/binny/blockchainProjects/trufflePro/solidity-note/build/contracts/BNB.json')
describe("BNB", function () {
    let web3;
    it('get web3', async function () {
        web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:7545'))
    });
    let accounts;
    it('所有账户信息', async function () {
        accounts = await web3.eth.getAccounts();
        console.log(accounts);
    });
    it('检查地址的合法性', async function () {
        let myiban = web3.eth.Iban.fromAddress(address)
        console.log(myiban)
    });
    let {
        abi,
    } = JSON.parse(source)

    it('合约abi ', async function () {
        console.log(abi);
    });
    let instance;
    it('获取合约实例', async function () {
        instance = new web3.eth.Contract(abi, address, {
            from: accounts[0]
        });
        // shelljs.echo(instance).to('./instance/instance1.log');
    });
    let from;
    it('设置默认 from 账户', async function () {
        /**
         * 从默认账户转钱
         */
        from = accounts[2];
        web3.eth.defaultAccount = from;
        console.log(from);
    });
    it('合约默认账户', async function () {
        console.log(web3.eth.Contract.defaultAccount);
    });
    it('address', async function () {
        console.log(instance.options.address);
    });
    it('账户余额 ', async function () {
        let balance = await web3.eth.getBalance(from);
        console.log(balance);
    });
    it('转账(在线签)', async function () {
        let transactionReceipt = await web3.eth.sendTransaction({
            from:accounts[0],
            to: address,
            value: web3.utils.toWei('3', 'ether')
        });
        console.log(transactionReceipt);

    });
    let nonce;
    it('当前账户的 nonce', async function () {
        nonce = await web3.eth.getTransactionCount(accounts[0]);
        console.log(nonce);
    });
    const token_hex = '0x';
    it('离线签', async function () {
        let priv = "f2cec71fe3ae2df8ec55d11eb1d6c666bfdd5f5f02e7d19b85d990fbcfdbf99c";
        let privateKey = Buffer.from(priv, 'hex');
        let priceWei = await web3.eth.getGasPrice();
        priceWei = token_hex + Number(priceWei).toString(16);
        const gasLimitCounts = token_hex + Number(30000).toString(16);
        const amountWei = web3.utils.toWei('3', 'ether');
        // log('amountWei = ' + amountWei);
        const amount = token_hex + Number(amountWei).toString(16);
        let rawTx = {
            nonce: nonce,
            gasPrice: priceWei.toString(),
            gasLimit: gasLimitCounts.toString(),
            to: "0xdECb6518Af4a1b20BB9E4edC04Ff521f58E3C492",
            value: amount.toString(),
            data: '0x00'
        };
        let tx = new Tx(rawTx);
        tx.sign(privateKey);
        let serializedTx = tx.serialize();
        let transactionReceipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
        console.log(transactionReceipt);
    });
    it('获取 from 余额', async function () {
        let balance = await web3.eth.getBalance(from);
        console.log(balance);
    });
    it('当前块高', async function () {
        let number = await web3.eth.getBlockNumber();
        console.log(number);
    });
    it('methods ', async function () {
        console.log(instance.methods)
    });
    it('转以太币', async function () {
        let age = await instance.methods.withdrawEther(web3.utils.toWei('0', 'ether')).send();
        console.log(age);
    });
    it('查询 eth', async function () {
        let age = await instance.methods.getEth().call({from:accounts[8]});
        console.log(age);
        console.log(web3.utils.fromWei(age, 'ether'));
    });
    it('授权', async function () {
        let age = await instance.methods.approve(accounts[4], 5).send();
        console.log(age);
    });
    it('查询授权', async function () {
        let age = await instance.methods.getAllowanceValue(accounts[0]).call({from: accounts[4]});
        console.log(age);
    });
    it('代理转账', async function () {
        let newVar = await instance.methods.transferFrom(accounts[0], accounts[3], 1).send({
            from: accounts[4]
        });
        console.log(newVar);
    });
    // it('读数据', async function () {
    //     let age = await instance.methods.getAge().call();
    //     console.log(age);
    // });
    // it('获取 from 余额', async function () {
    //     let balance = await web3.eth.getBalance(from);
    //     console.log(balance);
    // });
});